<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    public $timestamps = true;

    protected $table = 'users';

    /**
     * 元数据修改器
     *
     * @param  string  $value
     * @return void
     */
    public function setOriginalAttribute($value)
    {
        $this->attributes['original'] = json_encode($value , JSON_UNESCAPED_UNICODE);
    }

    /**
     *  元数据访问器.
     *
     * @param  string  $value
     * @return string
     */
    public function getOriginalAttribute($value)
    {
        return json_decode($value , true);
    }


    /**
     * 为数组 / JSON 序列化准备日期。
     *
     * @param \DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(\DateTimeInterface $date)
    {
        return Carbon::instance($date)->toDateTimeString();
    }
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'open_id','id_card' ,'phone','name','nick_name','money','parent_user_id','avatar','original','code','is_qrcode'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'jwt_token',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     * 邀请人信息
     */
    public function parent()
    {
        return $this->hasOne(Users::class , 'id' , 'parent_user_id');
    }
}
